#################################################################################
### Racialized Misinformation, Factual Corrections, and Prejudicial Attitudes ###
### [Minimum Detectable Effects Analysis for Study 3]                         ###
### Authors: Eddy S. F. Yeung, Joseph Glasgow                                 ###
### Date: June 26, 2025                                                       ###
#################################################################################

### Set-up ----
## Clean the working environment and set the working directory
rm(list = ls())
setwd("~/Desktop/racialized_misinfo/replication/Study 3") # set your working directory here, which should also contain the survey data ("study3_dataset.csv")

## Import the dataset
df <- read.csv("study3_dataset.csv")

### Code outcome variables ----
## Affective prejudice (0 = most favorable and warm; 100 = least favorable and warm)
df$affective_prej <- 100 - df$affect_1

## Desire for social distance (0 = min; 100 = max)
df$social_distance <- 15 - (df$social_distance_1 + df$social_distance_2 + df$social_distance_3)
df$behavioral_prej <- df$social_distance / 3 * 25

### Conduct minimum detectable effects analysis on affective prejudice ----
## Calculate the SD of the variable in the control group
sd.ctrl <- sd(df$affective_prej[df$immi_treatment == 0], na.rm = T)
sd.ctrl

## Calculate the SD of the variable in the treatment group
sd.treat <- sd(df$affective_prej[df$immi_treatment == 1], na.rm = T)
sd.treat

## Calculate the sample size in the control group
n.ctrl <- length(df$affective_prej[df$immi_treatment == 0][!is.na(df$affective_prej[df$immi_treatment == 0])])
n.ctrl

## Calculate the sample size in the treatment group
n.treat <- length(df$affective_prej[df$immi_treatment == 1][!is.na(df$affective_prej[df$immi_treatment == 1])])
n.treat

## MDE calculation
# The true effect size must be at least 2.8 standard errors from zero to detect 
# it with 80% probability using 95% confidence intervals (Gelman and Hill 2006).
# To estimate the standard error of the ATE, we use equation 3.6 in Gerber and 
# Green (2012). Thus, we simply multiply 2.8 the standard error of the ATE to 
# calculate the MDE.
MDE <- 2.8 * sqrt((sd.ctrl^2/(n.ctrl/2)) + (sd.treat^2/(n.treat/2)))
MDE / sd(df$affective_prej, na.rm = T) # MDE in Cohen's d

### Conduct minimum detectable effects analysis on desire for social distance ----
## Calculate the SD of the variable in the control group
sd.ctrl <- sd(df$behavioral_prej[df$immi_treatment == 0], na.rm = T)
sd.ctrl

## Calculate the SD of the variable in the treatment group
sd.treat <- sd(df$behavioral_prej[df$immi_treatment == 1], na.rm = T)
sd.treat

## Calculate the sample size in the control group
n.ctrl <- length(df$behavioral_prej[df$immi_treatment == 0][!is.na(df$behavioral_prej[df$immi_treatment == 0])])
n.ctrl

## Calculate the sample size in the treatment group
n.treat <- length(df$behavioral_prej[df$immi_treatment == 1][!is.na(df$behavioral_prej[df$immi_treatment == 1])])
n.treat

## MDE calculation
# The true effect size must be at least 2.8 standard errors from zero to detect 
# it with 80% probability using 95% confidence intervals (Gelman and Hill 2006).
# To estimate the standard error of the ATE, we use equation 3.6 in Gerber and 
# Green (2012). Thus, we simply multiply 2.8 the standard error of the ATE to 
# calculate the MDE.
MDE <- 2.8 * sqrt((sd.ctrl^2/(n.ctrl/2)) + (sd.treat^2/(n.treat/2)))
MDE / sd(df$behavioral_prej, na.rm = T) # MDE in Cohen's d
